End.

js的双问号和"?."的含义和使用


?. 表示:可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每 个引用是否有效。操作符的功能类似于 . 链式操作符,不同之处在于,在引用为空(null 或者 undefined) 的情况下不会引起错误,该表达式短路返回值。

?. 是判断对象的某个属性是否存在,如果存在那么就返回整个属性的值,否则返回undefined


?? 表示:只有左侧的值为null或undefined的时候才使用右侧的值。



const obj = {
     name: 'test',
     detail: {
       dog: 'xiaobai'
     }
}
const name = obj.dog ?. name;
console.log(name) // undefined

因为obj不存在dog属性,所以查找dog下面的name根本没有所以返回undefined


const detail = obj.detail ?. dog;
console.log(detail) // xiaobai


const name = obj.dog ?. name ?? 'moren';
console.log(name) // moren
End.